﻿<?
class controller{
	private $view;
	private $model;
	private $ref;
	private $sms;
	
	function controller($ref){
		$this->init($ref);
		
	}
	function init($ref){
		$this->ref=$ref;
		$request = user::request();
		$this->model=new model();
		
		if(isset($request["sent"])){
			$this->model->addXhtml($request["xhtml"]);
			$this->model->addXsl($request["xsl"]);
			var_dump($this->model->getData());exit;
			if ($this->sms = $this->model->getData()){
				
				$this->view = $this->validate($request);
			}
		}else
			$this->view = new formview($this->ref->file);
			
		
		
	}
	function validate($request){
		
		if ( isset($request['phone']) ){
			$phone= ereg_replace("[^0-9]*","",$request['phone']);
			$sendMessage = $this->model->sendSms(MSISDN,CLAVE,$phone,$this->sms);
			switch( $sendMessage ){
				case "OK":
				return new goodview($this->ref->file);
				break;
				
				default:
				$message=$this->extractMessage($sendMessage);
			}
		}
		
		return new badview($this->ref->file, $message);
	}
	function extractMessage($sendMessage){
		
		$arr=explode("-",$sendMessage);
		$message=Array("code"=>$arr[0],"description"=>$arr[1]);
		return $message;
		
	}
	function render(){
		return $this->view->render();
	}
	

	function sendError(){
	
	}
}
?>